var t, a = require("../../../../@babel/runtime/helpers/interopRequireDefault")(require("../../../../@babel/runtime/regenerator")), e = require("../../../../@babel/runtime/helpers/asyncToGenerator"), n = getApp();

Page({
    data: {
        tabActive: "2",
        isReceive: !1,
        showDialog: !1,
        recordList: [],
        can_total: 0,
        stored_total: 0,
        gain_total: 0,
        past_total: 0,
        titleText: "",
        loading: !1,
        finished: !1,
        page: 1,
        redpack: {},
        audio: {},
        audioContext: null
    },
    onLoad: (t = e(a.default.mark(function t(e) {
        return a.default.wrap(function(t) {
            for (;;) switch (t.prev = t.next) {
              case 0:
                return t.next = 2, this.getAudio();

              case 2:
                this.createAudio();

              case 3:
              case "end":
                return t.stop();
            }
        }, t, this);
    })), function(a) {
        return t.apply(this, arguments);
    }),
    createAudio: function() {
        var t = wx.createInnerAudioContext();
        t.src = this.data.audio.voice_link, t.autoplay = !1, wx.setInnerAudioOption ? wx.setInnerAudioOption({
            obeyMuteSwitch: !1
        }) : t.obeyMuteSwitch = !1, this.setData({
            audioContext: t
        });
    },
    onShow: function() {
        this.getData();
    },
    switchTab: function(t) {
        this.setData({
            tabActive: t.detail.name
        }), this.initData(), this.getData();
    },
    getData: function() {
        var t = this;
        if (!this.data.finished) {
            var a = n.getNetAddresss("plugin.love-redpack.frontend.redpack.homePage");
            n._getNetWork({
                url: a,
                data: {
                    status: this.data.tabActive,
                    page: this.data.page
                },
                success: function(a) {
                    if (1 === a.data.result) {
                        var e = a.data.data.list, n = a.data.data, i = [];
                        (e.last_page === t.data.page || e.data.length < e.per_page) && t.setData({
                            finished: !0
                        }), i = t.data.page > 1 ? t.data.recordList.concat(e.data) : e.data, wx.setNavigationBarTitle({
                            title: n.love_name
                        }), t.setData({
                            page: ++t.data.page,
                            can_total: n.can_total,
                            stored_total: n.stored_total,
                            gain_total: n.gain_total,
                            past_total: n.past_total,
                            recordList: i,
                            isReceive: n.can_total <= 1,
                            titleText: n.love_name
                        });
                    } else wx.showModal({
                        showCancel: !1,
                        content: a.data.msg,
                        success: function() {}
                    });
                }
            });
        }
    },
    getAudio: function() {
        var t = this, a = n.getNetAddresss("plugin.love-redpack.frontend.voice.index");
        return new Promise(function(e, i) {
            n._getNetWork({
                url: a,
                data: {},
                success: function(a) {
                    if (1 !== a.data.result) return wx.showModal({
                        showCancel: !1,
                        content: a.data.msg,
                        success: function() {}
                    }), void i(a.data.data);
                    t.setData({
                        audio: a.data.data
                    }), e(a.data.data);
                }
            });
        });
    },
    getRedpack: function() {
        var t = this, a = n.getNetAddresss("plugin.love-redpack.frontend.redpack.getRedpack");
        return new Promise(function(e, i) {
            n._getNetWork({
                url: a,
                data: {},
                success: function(a) {
                    if (1 !== a.data.result) return wx.showModal({
                        showCancel: !1,
                        content: a.data.msg,
                        success: function() {}
                    }), void e(a.data.result);
                    t.setData({
                        redpack: a.data.data
                    }), e(a.data.result);
                },
                fail: function(t) {
                    i(t);
                }
            });
        });
    },
    showPopup: function() {
        var t = this;
        return e(a.default.mark(function e() {
            return a.default.wrap(function(a) {
                for (;;) switch (a.prev = a.next) {
                  case 0:
                    if (!(t.data.can_total <= 0)) {
                        a.next = 2;
                        break;
                    }
                    return a.abrupt("return");

                  case 2:
                    return a.next = 4, t.getRedpack();

                  case 4:
                    if (1 === a.sent) {
                        a.next = 7;
                        break;
                    }
                    return a.abrupt("return");

                  case 7:
                    t.data.audioContext && t.data.audio.is_open && (t.data.audioContext.seek(0), t.data.audioContext.play()), 
                    t.setData({
                        showDialog: !0,
                        can_total: --t.data.can_total
                    });

                  case 9:
                  case "end":
                    return a.stop();
                }
            }, e);
        }))();
    },
    onClose: function() {
        this.data.audioContext.pause(), this.initData(), this.getData(), this.setData({
            showDialog: !1
        });
    },
    initData: function() {
        this.setData({
            showDialog: !1,
            recordList: [],
            loading: !1,
            finished: !1,
            page: 1
        });
    },
    onUnload: function() {
        this.data.audioContext.stop();
    }
});